//
//  PayTrustFeeViewController.m
//  Fangmm
//
//  Created by 阮琦 on 16/3/3.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "PayTrustFeeViewController.h"
#import "Utility.h"
#include "Constants.h"
#import "HttpServer.h"
#import "WebViewController.h"
#import "PersonAccountViewController.h"
#import <TSMessages/TSMessageView.h>
#import <MobClick.h>
@interface PayTrustFeeViewController ()
{
    int balance;
    int singleTrustee;
    int totalTrustee;
    int trustee;
}

@end

@implementation PayTrustFeeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadData];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES];
    [MobClick beginLogPageView:@"支付保证金页"];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO];
    [MobClick endLogPageView:@"支付保证金页"];
}
-(void)loadData
{
    [HttpServer getTrusteesWithScheduleId:_scheduleId WithSuccessBlock:^(NSDictionary *responseData)
    {
        NSLog(@"++++%@",responseData);
        balance = [responseData[@"balance"]intValue];
        singleTrustee = [responseData[@"singleTrustee"]intValue];
        totalTrustee = [responseData[@"totalTrustee"]intValue];
        trustee = [responseData[@"trustee"]intValue];
        NSLog(@"balance=%d,singleTrustee=%d,totalTrustee=%d",balance,singleTrustee,totalTrustee);
        self.houseNum.text = [NSString stringWithFormat:@"*%d",(((totalTrustee/singleTrustee)))];
        self.didTrustee.text = [NSString stringWithFormat:@"%d",trustee/100];
        self.needTrustee.text = [NSString stringWithFormat:@"%d",(totalTrustee - trustee)/100];
        self.accountBalance.text = [NSString stringWithFormat:@"%d",balance/100 - trustee/100];
        NSLog(@"trustee = %d,didTrustee = %d,needTrustee = %d",trustee,trustee,(totalTrustee - trustee)/100);
        if (balance >= totalTrustee)
        {
            NSString *buttonTitle = [NSString stringWithFormat:@"确认充值托管金:%d",(totalTrustee - trustee)/100];
            self.payButtonView.tag = 0;
            [self.payButtonView setTitle:buttonTitle forState:UIControlStateNormal];
        }
        else
        {
            NSString *buttonTitle = @"前往个人帐户充值";
            self.payButtonView.tag = 1;
            [self.payButtonView setTitle:buttonTitle forState:UIControlStateNormal];
        }
    }
    withFailBlock:^(id errorMsg)
    {
        NSLog(@"%@",errorMsg);
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)payButtonClick:(UIButton *)sender
{
    
    if (sender.tag)
    {
        //个人帐户
        PersonAccountViewController *personAccount = [[PersonAccountViewController alloc]init];
        [self.navigationController pushViewController:personAccount animated:YES];
    }
    else
    {
        //充值保证金
        [HttpServer payTrusteesWithSchedule:_scheduleId WithSuccessBlock:^(NSDictionary *responseData)
        {
            NSLog(@"baozhengjin == %@",responseData);
            [TSMessage showNotificationInViewController:self title:responseData[@"msg"] subtitle:@"" type:TSMessageNotificationTypeSuccess duration:2.0 canBeDismissedByUser:true];
        }
        withFailBlock:^(id errorMsg)
        {
            NSLog(@"%@",errorMsg);
            [TSMessage showNotificationInViewController:self title:errorMsg[@"msg"] subtitle:@"" type:TSMessageNotificationTypeError duration:2.0 canBeDismissedByUser:true];
        }];
    }

}

- (IBAction)toBack:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
